package com.master.common.utils;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReflectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author Heath.Que
 * @version 1.0
 * @classname
 * @description TODO
 * @Date 2021/5/20 15:17
 **/
@Component
public class RedisUtil {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private RedisTemplate<String, Object> redisTemplate;

    public RedisUtil(){

    }

    public boolean expire(String key, long time){
        try {
            this.redisTemplate.expire(key, time, TimeUnit.SECONDS);
            return true;
        }catch (Exception e){
            this.logger.error(e.getMessage(), e);
            return false;
        }
    }

    public boolean expire(String key, long time, TimeUnit unit){
        try {
            this.redisTemplate.expire(key, time, unit);
            return true;
        }catch (Exception e){
            this.logger.error(e.getMessage(), e);
            return false;
        }
    }

    public long getExpire(String key){
        return this.redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    public boolean hasKey(String key){
        try {
            return this.redisTemplate.hasKey(key);
        }catch (Exception e){
            return false;
        }
    }

    public void del(String... key){
        if (key != null && key.length > 0){
            if (key.length == 1){
                this.redisTemplate.delete(key[0]);
            }else {
                this.redisTemplate.delete(String.valueOf(CollectionUtils.arrayToList(key)));
            }
        }
    }

    public Object get(String key){
        return key == null ? null : this.redisTemplate.opsForValue().get(key);
    }

    public String getStr(String key){
        Object value = this.get(key);
        return key == null ? null : String.valueOf(value);
    }

    public <T> T get(String key, Class<T> clazz){
        Object value = this.get(key);
        if (null == value){
            return null;
        }else {
            T targetObject = ReflectUtil.newInstance(clazz, new Object());
            BeanUtil.copyProperties(value, targetObject, new String[0]);
            return targetObject;
        }
    }

    public boolean set (String key, Object value){
        try {
            this.redisTemplate.opsForValue().set(key, value);
            return true;
        }catch (Exception e){
            this.logger.error(e.getMessage(), e);
            return false;
        }
    }

    public boolean set(String key, Object value, long time){
        try {
            if (time > 0){
                this.redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            }else {
                this.set(key, value);
            }

            return true;
        }catch (Exception e){
            this.logger.error(e.getMessage(), e);
            return false;
        }
    }

    public long incr(String key, long delta){
        if (delta < 0){
            throw new RuntimeException("递增因子必须大于0");
        }else {
            return this.redisTemplate.opsForValue().increment(key, delta);
        }
    }

    public long decr(String key, long delta){
        if (delta < 0){
            throw new RuntimeException("递减因子必须大于0");
        }else {
            return this.redisTemplate.opsForValue().decrement(key, delta);
        }
    }

    public Object hget(String key, String item){
        return this.redisTemplate.opsForHash().get(key, item);
    }

    public Map<Object, Object> hmget(String key){
        return this.redisTemplate.opsForHash().entries(key);
    }

    public boolean hmset(String key, Map<String, Object> map){
        try {
            this.redisTemplate.opsForHash().putAll(key, map);
            return true;
        }catch (Exception e){
            this.logger.error(e.getMessage(), e);
            return false;
        }
    }

    public boolean hmset(String key, Map<String, Object> map, long time){
        try {
            this.redisTemplate.opsForHash().putAll(key, map);
            if (time > 0){
                this.expire(key, time);
            }
            return true;
        }catch (Exception e){
            this.logger.error(e.getMessage(), e);
            return false;
        }
    }

    public boolean hset(String key, String item, Object value){
        try {
            this.redisTemplate.opsForHash().put(key, item, value);
            return true;
        }catch (Exception e){
            this.logger.error(e.getMessage(), e);
            return false;
        }
    }

    public boolean hset(String key, String item, Object value, long time){
        try {
            this.redisTemplate.opsForHash().put(key, item, value);
            if (time > 0){
                this.expire(key, time);
            }
            return true;
        }catch (Exception e){
            this.logger.error(e.getMessage(), e);
            return false;
        }
    }

    public void hdel(String key, Object... item){
        this.redisTemplate.opsForHash().delete(key, item);
    }

    public boolean hHasKey(String key, String item){
        return this.redisTemplate.opsForHash().hasKey(key, item);
    }

    public double hincr(String key, String item, double by){
        return this.redisTemplate.opsForHash().increment(key, item, by);
    }

    public double hdecr(String key, String item, double by){
        return this.redisTemplate.opsForHash().increment(key, item, -by);
    }

    public Long leftPush(String key, Object value){
        return this.redisTemplate.opsForList().leftPush(key, value);
    }

    public Long leftPushAll(String key, Object value){
        return this.redisTemplate.opsForList().leftPushAll(key, value);
    }

    public Object leftPop(String key){
        return this.redisTemplate.opsForList().leftPop(key);
    }

    public Object rightPush(String key, Object value){
        return this.redisTemplate.opsForList().rightPush(key, value);
    }

    public Long rightPushAll(String key, Object... values){
        return this.redisTemplate.opsForList().rightPushAll(key, values);
    }

    public Object rightPop (String key){
        return this.redisTemplate.opsForList().rightPop(key);
    }

    public Object rightPop(String key, long timeout, TimeUnit timeUnit){
        return this.redisTemplate.opsForList().rightPop(key, timeout, timeUnit);
    }

    public Long listSize(String key){
        return this.redisTemplate.opsForList().size(key);
    }

    public List<Object> listGet(String key, long start, long end){
        return this.redisTemplate.opsForList().range(key, start, end);
    }

    public Object listGetIndex(String key, long index){
        return this.redisTemplate.opsForList().index(key, index);
    }

    public boolean listUpdateIndex(String key, long index, String value){
        try {
            this.redisTemplate.opsForList().set(key, index, value);
            return true;
        }catch (Exception e){
            this.logger.error(e.getMessage(), e);
            return false;
        }
    }

    public boolean listDel(String key, long count, String value){
        try {
            this.redisTemplate.opsForList().remove(key, count, value);
            return true;
        }catch (Exception e){
            this.logger.error(e.getMessage(), e);
            return false;
        }
    }

    public void convertAndSend(String channel, Object message){
        this.redisTemplate.convertAndSend(channel, message);
    }

    public Object getMessageObject(Message message){
        Object value = this.redisTemplate.getValueSerializer().deserialize(message.getBody());
        return value;
    }
}
